/**
 * @description 参数校验
 */

async function validateParams(ctx, next) {
  ctx.validate = function (schema, data) {
    const method = ctx.request.method;
    if (!data) {
      if (method === 'POST') {
        data = ctx.request.body;
      } else {
        data = ctx.request.query;
      }
    }
    const { error } = schema.validate(data);
    if (error) {
      ctx.throwValidateError(error);
    }
  };
  return await next();
}

module.exports = validateParams;